package com.example.satu.quizcreator.quiz_creator;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import com.example.satu.quizcreator.quiz.Quiz;
import com.example.satu.quizcreator.R;
import com.example.satu.quizcreator.quiz_creator.interfaces.onWarningThrow;
import com.github.rtyley.android.sherlock.roboguice.fragment.RoboSherlockFragment;

import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;

import roboguice.inject.InjectView;

/**
 * Created by Satu on 2014-09-03.
 */
public class quiz_details_fragment extends RoboSherlockFragment implements onWarningThrow {

    public static final int OFFLINE_COLOR = android.R.color.darker_gray;
    @InjectView(R.id.editText_quiz_name)
    EditText editTextQuizname;

    @InjectView(R.id.checkBox_normal_quiz)
    CheckBox checkBoxNormal;

    @InjectView(R.id.checkBox_exam_quiz)
    CheckBox checkBoxExam;

    @InjectView(R.id.editText_email)
    EditText editText_Email;

    @InjectView(R.id.textView_email)
    TextView textView_Email;

    @InjectView(R.id.imageButton_arrows)
    ImageButton imageButton_arrows;

    @InjectView(R.id.numberPicker_question_number)
    com.example.satu.quizcreator.helpers.NumberPicker numberPickerQuestionNumber;


    @InjectView(R.id.numberPicker_time_to_end)
    com.example.satu.quizcreator.helpers.NumberPicker numberPickerTime;

    @InjectView(R.id.textView_warning_massage)
    TextView textView_warning_massage;


    Quiz.MODE quizMode = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.quiz_details_fragment,null);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        Animation hyperspaceJumpAnimation = AnimationUtils.loadAnimation(getActivity(), R.anim.animfile);
        imageButton_arrows.startAnimation(hyperspaceJumpAnimation);

        checkBoxExam.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (!isChecked) {
                    textView_Email.setTextColor(getResources().getColor(OFFLINE_COLOR));
                    editText_Email.setEnabled(false);
                    removeMode(Quiz.MODE.EXAM_MODE);
                } else {
                    textView_Email.setTextColor(getResources().getColor(android.R.color.black));
                    editText_Email.setEnabled(true);
                    setMode(Quiz.MODE.EXAM_MODE);
                }
            }
        });
        checkBoxNormal.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                if (!isChecked)
                    removeMode(Quiz.MODE.NORMAL_MODE);
                else
                    setMode(Quiz.MODE.NORMAL_MODE);

            }
        });
    }

    public Quiz getQuiz(){
        if (!quizVerification())
            return null;
        Quiz quiz = new Quiz();
        quiz.setName(editTextQuizname.getText().toString());
        quiz.setMode(quizMode);
        quiz.setQuestionNumber(numberPickerQuestionNumber.getValue());
        quiz.setTime(numberPickerTime.getValue());
        if(quizMode == Quiz.MODE.EXAM_MODE || quizMode ==  Quiz.MODE.EXAM_AND_NORMAL_MODE)
            quiz.setMail(editText_Email.getText().toString());
        return quiz;
    }
    private void setMode(Quiz.MODE mode){
        if(quizMode != null)
            quizMode = Quiz.MODE.EXAM_AND_NORMAL_MODE;
        else
            quizMode = mode;
    }
    private void removeMode(Quiz.MODE mode){
        if(quizMode == Quiz.MODE.EXAM_AND_NORMAL_MODE){
            if(mode == Quiz.MODE.EXAM_MODE)
              quizMode   = Quiz.MODE.NORMAL_MODE;
            else
                quizMode = Quiz.MODE.EXAM_MODE;
        }else
            quizMode = null;
    }
    public boolean quizVerification(){
        textView_warning_massage.setText("");
        boolean testResult = true;
        if (isEmpty(editTextQuizname)) {
            testResult = false;
            textView_warning_massage.append("Nazwa quizu nie może być pusta ");
        }
        if (numberPickerQuestionNumber.getValue() == 0){
            testResult = false;
            textView_warning_massage.append(" Wprowadź ilość pytań ");
        }
        if (numberPickerTime.getValue() == 0 ){
            testResult = false;
            textView_warning_massage.append(" Wprowadź czas quizu");
        }
        if (!checkBoxExam.isChecked() && !checkBoxNormal.isChecked()){
            testResult = false;
            textView_warning_massage.append(" Musisz wybrać tryb");
        }
        if (checkBoxExam.isChecked()){
            if (!isValidEmailAddress(editText_Email.getText().toString())){
                testResult = false;
                textView_warning_massage.append(" Adres email jest nie poprawny");
            }
        }
        if (!testResult)
            textView_warning_massage.setVisibility(View.VISIBLE);
        return testResult;
    }
    private boolean isEmpty(EditText etText) {
        return etText.getText().toString().trim().length() <= 0;
    }
/*    if (etText.getText().toString().trim().length() > 0) {
        return false;
    } else {
        return true;
    }*/
/*    private boolean isNumbers(String s){
        try {
            Integer.parseInt(s);
        } catch(NumberFormatException e) {
            return false;
        }
        return true;
    }*/
    private boolean isValidEmailAddress(String email) {
        boolean result = true;
        try {
            InternetAddress emailAddr = new InternetAddress(email);
            emailAddr.validate();
        } catch (AddressException ex) {
            result = false;
        }
        return result;
    }


    @Override
    public void warningThrow(String s) {
        if(textView_warning_massage != null)
            textView_warning_massage.append(s);
    }
}
